//
//  ViewController.m
//  KVO--属性依赖
//
//  Created by 岚海网络 on 2018/12/4.
//  Copyright © 2018年 岚海网络. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@property (nonatomic , strong) Person * person;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _person = [[Person alloc] init];
    
    /* 注册 */
    [_person addObserver: self forKeyPath:@"dog" options:(NSKeyValueObservingOptionNew) context:nil];
    
}

/* 监听方法 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSLog(@"%@\n 狗的年龄是%d \n 狗的等级是%d",change,_person.dog.age,_person.dog.level);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    static int a;
    _person.dog.age = a++;
    _person.dog.level = a++;
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"dog.age"];
}

@end
